import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../view/OneRouter/Login.vue'
import Home from '../view/OneRouter/Hmoe.vue'

import GoodAside from '../view/TwoRouter/GoodsAside/GoodAside.vue'

import RunAside from '../view/TwoRouter/RunAside/RunAside.vue'
import AdvertisingList from '../view/TwoRouter/RunAside/AdvertisingList.vue'
import Advertising from '../view/TwoRouter/RunAside/Advertising.vue'

import WeCome from '../view/TwoRouter/WeCome.vue'

import GoodsPage from '../view/ThreeRouter/Goods/Goods.vue'
import GoodsAdd from '../view/ThreeRouter/Goods/GoodsAdd.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    // home主页路由
    path: '/home',
    component: Home,
    // 侧边栏路由
    children: [
      {
        path: '/home',
        component: WeCome,
        // 展示区路由
        children: [
          {
            path: '/home',
            component: WeCome
          }
        ]
      },
      {
        // 商品
        path: '/goods',
        component: GoodAside,
        redirect: '/goods/goodsList',
        children: [
          // 商品列表
          {
            path: '/goods/goodsList',
            component: GoodsPage
          },
          // 添加商品
          {
            path: '/goods/addGoods',
            component: GoodsAdd
          }
        ]
      },
      {
        // 运营
        path: '/runs',
        component: RunAside,
        children: [
          {
            path: '/runs/adlist',
            component: AdvertisingList
          },
          {
            path: '/runs/Advertising',
            component: Advertising
          }
        ]
      },
      {
        path: '/all',
        component: WeCome
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to)
  console.log(from, 'from')

  if (to.path === '/login') next()

  let ToKen = window.localStorage.getItem('token')
  console.log(ToKen)

  if (ToKen) {
    next()
  } else {
    next('/login')
  }
})

export default router
